package com.kuaidu.ll.quickreadnews.widget.common_ui;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.RadioButton;

import com.kuaidu.ll.quickreadnews.R;
import com.kuaidu.ll.quickreadnews.utils.ContextUtils;

/**
 * Created by liulei on 2017/7/13.
 * 自定义radioButton
 */

public class KDRadioButton extends RadioButton {
    private static final String TAG = KDRadioButton.class.getSimpleName();

    public static final float XRATIO = 0.7F;
    public static final float YRATIO = 0.7F;
    public static final float CIRCLESPOTSIZE = 2.5F;
    public static final int CIRCLESPOTCOLOR = R.color.red_circle_spot;

    private Context context;
    private Drawable customDrawable;
    private boolean showCircleSpot;
    private int circleSpotColor;
    private float circleSpotSize;
    private float buttonImageHeight;
    private float buttonImageWidth;
    private Paint mPaint;
    private float xRatio;
    private float yRatio;

    public KDRadioButton(Context context) {
        this(context, null);
    }

    public KDRadioButton(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public KDRadioButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init(attrs, defStyleAttr);
        initButton();
    }

    private void init(AttributeSet attrs, int defStyleAttr) {
        mPaint = new Paint();
        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.KDRadioButton, 0, 0);
        try {
//            customDrawable = typedArray.getDrawable(R.styleable.KDRadioButton_buttonImage);
            showCircleSpot = typedArray.getBoolean(R.styleable.KDRadioButton_circleSpotEnable, false);
            circleSpotColor = typedArray.getColor(R.styleable.KDRadioButton_circleSpotColor, ContextCompat.getColor(context, CIRCLESPOTCOLOR));
            circleSpotSize = typedArray.getFloat(R.styleable.KDRadioButton_circleSpotSize, CIRCLESPOTSIZE);
            xRatio = typedArray.getFloat(R.styleable.KDRadioButton_xRatio, XRATIO);
            yRatio = typedArray.getFloat(R.styleable.KDRadioButton_yRatio, YRATIO);

//            buttonImageWidth = typedArray.getFloat(R.styleable.KDRadioButton_buttonImageWidth,0.0F);
//            buttonImageHeight = typedArray.getFloat(R.styleable.KDRadioButton_buttonImageHeight,0.0F);
        } catch (Exception e) {
            Log.e(TAG, "init: " + e.getMessage(), e);
        } finally {
            typedArray.recycle();
        }
    }

    private void initButton() {
        mPaint.setAntiAlias(true);
        mPaint.setColor(circleSpotColor);
        mPaint.setStyle(Paint.Style.FILL);
    }

    public void setCircleSpotEnable(boolean isShow) {
        this.showCircleSpot = isShow;
        invalidate();
    }

//    public void setButtonImage(Drawable drawable,float size){
//        this.customDrawable = drawable;
//        this.circleSpotSize = size;
//        invalidate();
//    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        if (customDrawable != null) {
//            Drawable drawable = customDrawable;
//            int top = getPaddingTop();
//            int bottom = 0;
//            if (buttonImageHeight == 0) {
//                bottom = getHeight() + top;
//            } else {
//                bottom = (int) buttonImageHeight + top;
//            }
//            int left = getPaddingLeft();
//            int right = 0;
//            if (buttonImageWidth == 0) {
//                right = left + getWidth();
//            } else {
//                right = left + (int)buttonImageWidth;
//            }
//            drawable.setBounds(left, top, right, bottom);
//            customDrawable.draw(canvas);
//        }

        if (showCircleSpot) {
            float cX = (getPaddingLeft() + getWidth() + getPaddingRight()) * xRatio;
            float cY = (getPaddingTop() + getHeight() + getPaddingBottom()) * yRatio;
            int cS = ContextUtils.dip2px(context, circleSpotSize);
            canvas.drawCircle(cX, cY, cS, mPaint);
        }
    }

//    @Override
//    protected void drawableStateChanged() {
//        super.drawableStateChanged();
//        final Drawable buttonDrawable = customDrawable;
//        if (buttonDrawable != null && buttonDrawable.isStateful()&&buttonDrawable.setState(getDrawableState())){
//            invalidate();
//        }
//    }
}
